﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using KaraokeManagementBUS;
using KaraokeManagementDTO;

namespace KaraokeManagement
{
    public partial class ucQuanLyPhongHat : UserControl
    {
        public static string staticTenPhong="";
        public static int staticMaPhong=-1;
        public static int staticMaPhieuDatPhong = -1;

        public ucQuanLyPhongHat()
        {
            InitializeComponent();
        }
    
        private void ucQuanLyPhongHat_Load(object sender, EventArgs e)
        {
            loadDanhSachPhongHat();            
        }

       

        public void loadDanhSachPhongHat()
        {
            List<PhongHatDTO> list = new List<PhongHatDTO>();
            list = new PhongHatBUS().layDanhSachPhongHat();

            lvDanhSachPhong.Items.Clear();

            if (list == null)
                return;

            foreach (PhongHatDTO dto in list)
            {
                ListViewItem item = new ListViewItem();

                item.Text = dto.TenPhong;
                item.Tag = dto;
                item.ImageIndex = (dto.TinhTrangPhong == 0 ? 0 : 1);

                lvDanhSachPhong.Items.Add(item);
            }
        }


        
        private void cbTang_SelectedIndexChanged(object sender, EventArgs e)
        {
            loadDanhSachPhongHat();
        }

        private void btnLapPhieuDatPhong_Click(object sender, EventArgs e)
        {
            if (lvDanhSachPhong.SelectedIndices.Count > 0)
            {
                frmPhieuTiepNhanPhong frm = new frmPhieuTiepNhanPhong();
                frm.ShowDialog();

                loadDanhSachPhongHat();
            }
        }

        

        private void lvDanhSachPhong_DoubleClick(object sender, EventArgs e)
        {
            if (lvDanhSachPhong.SelectedIndices.Count > 0)
            {
                ListViewItem item = new ListViewItem();
                item = lvDanhSachPhong.SelectedItems[0];

                PhongHatDTO p = new PhongHatDTO();
                p = (PhongHatDTO)item.Tag;

                if (p.TinhTrangPhong == 1)
                {
                    //Lấy phiếu đặt phòng gần nhất
                    staticMaPhieuDatPhong = new PhieuTiepNhanDatPhongBUS().layPhieuDatPhongHienTai(p.ID);
                }

                staticMaPhong = p.ID;
                staticTenPhong = item.Text;
            }

            frmPhieuTiepNhanPhong frm = new frmPhieuTiepNhanPhong();
            frm.ShowDialog();
        }

        private void btnCapNhat_Click(object sender, EventArgs e)
        {
            if (lvDanhSachPhong.SelectedIndices.Count > 0)
            {
                ListViewItem item = new ListViewItem();
                item = lvDanhSachPhong.SelectedItems[0];

                PhongHatDTO p = new PhongHatDTO();
                p = (PhongHatDTO)item.Tag;

                if (p.TinhTrangPhong == 1)
                { 
                    //Lấy phiếu đặt phòng gần nhất
                    staticMaPhieuDatPhong = new PhieuTiepNhanDatPhongBUS().layPhieuDatPhongHienTai(p.ID);
                }
                                
                staticMaPhong = p.ID;
                staticTenPhong = item.Text;
            }

            frmPhieuTiepNhanPhong frm = new frmPhieuTiepNhanPhong();
            frm.ShowDialog();
        }

        private void buttonX3_Click(object sender, EventArgs e)
        {
            if (lvDanhSachPhong.SelectedIndices.Count > 0)
            {
                ListViewItem item = new ListViewItem();
                item = lvDanhSachPhong.SelectedItems[0];

                PhongHatDTO p = new PhongHatDTO();
                p = (PhongHatDTO)item.Tag;

                if (p.TinhTrangPhong == 1)
                {
                    //Lấy phiếu đặt phòng gần nhất
                    staticMaPhieuDatPhong = new PhieuTiepNhanDatPhongBUS().layPhieuDatPhongHienTai(p.ID);
                }               
            }

            frmPhieuTinhTien frm = new frmPhieuTinhTien();
            frm.ShowDialog();
        }

        private void lvDanhSachPhong_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lvDanhSachPhong.SelectedIndices.Count > 0)
            {
                ListViewItem item = new ListViewItem();
                item = lvDanhSachPhong.SelectedItems[0];

                PhongHatDTO p = new PhongHatDTO();
                p = (PhongHatDTO)item.Tag;
                staticMaPhong = p.ID;
                staticTenPhong = item.Text;

                if (p.TinhTrangPhong == 0)
                {
                    btnLapPhieuDatPhong.Enabled = true;
                    btnCapNhat.Enabled = false;
                    btnThanhToan.Enabled = false;
                }
                else
                {
                    btnLapPhieuDatPhong.Enabled = false;
                    btnCapNhat.Enabled = true;
                    btnThanhToan.Enabled = true;
                }
            }
        }

        private void btnChuyenTrangThai_Click(object sender, EventArgs e)
        {
            if (lvDanhSachPhong.SelectedIndices.Count > 0)
            {
                ListViewItem item = new ListViewItem();
                item = lvDanhSachPhong.SelectedItems[0];

                PhongHatDTO p = new PhongHatDTO();
                p = (PhongHatDTO)item.Tag;

                PhongHatBUS bus = new PhongHatBUS();
                if (p.TinhTrangPhong == 0)
                {
                    bus.capNhatTinhTrangPhongHat(p.ID, 1);
                }
                else
                {
                    bus.capNhatTinhTrangPhongHat(p.ID, 0);
                }

                loadDanhSachPhongHat();
            }
            
        }

       
        


        
    }
}
